"use strict";

(function () {
  function r(e, n, t) {
    function o(i, f) {
      if (!n[i]) {
        if (!e[i]) {
          var c = "function" == typeof require && require;
          if (!f && c) return c(i, !0);
          if (u) return u(i, !0);
          var a = new Error("Cannot find module '" + i + "'");
          throw a.code = "MODULE_NOT_FOUND", a;
        }

        var p = n[i] = {
          exports: {}
        };
        e[i][0].call(p.exports, function (r) {
          var n = e[i][1][r];
          return o(n || r);
        }, p, p.exports, r, e, n, t);
      }

      return n[i].exports;
    }

    for (var u = "function" == typeof require && require, i = 0; i < t.length; i++) {
      o(t[i]);
    }

    return o;
  }

  return r;
})()({
  1: [function (require, module, exports) {
    "use strict";
    /**
     * 底部导航TAB模板
     * 默认模块名: main
     * @return {[object]}  [ 返回一个对象 ]
     */

    loader.define({
      depend: ['pages/config/config'],
      loaded: function loaded(config, require, exports, module) {
        this.pageview = {}; // pageview.bind = function () {        
        // }

        this.pageview.init = function () {
          // // 绑定事件
          // this.bind();
          // 手机号,帐号是同个样式名, 获取值的时候,取的是最后一个focus的值
          // console.log(config.apiurl);
          var userInput = bui.input({
            id: ".user-input",
            callback: function callback(e) {
              // 清空数据
              this.empty();
            }
          }); // 密码显示或者隐藏

          var password = bui.input({
            id: ".password-input",
            iconClass: ".icon-eye",
            callback: function callback(e) {
              //切换类型
              this.toggleType();
              $(e.target).toggleClass("active");
            }
          }); // 手机号检查函数

          var phonecheck = function phonecheck(phonenum) {
            // 总共11位（第1位：1 第2位：3~9）
            var rule = /^1[3-9]\d{9}$/;
            if (!rule.test(phonenum)) return false;
            return true;
          }; //忘记密码


          $("#btn-forget").on("click", function (e) {
            bui.load({
              url: "pages/pwdreset/pwdreset"
            });
          });

          var login = function login(data, issave) {
            var data_ = JSON.stringify(data);
            bui.ajax({
              // url: 相对路径
              url: config.apiurl + "api/v1.0/login",
              data: data_,
              method: "post",
              headers: {
                'Content-Type': 'application/json'
              }
            }).then(function (res) {
              var msg = res['errmsg'];
              console.log(res);
              var ans = res['ans'];
              console.log(ans);

              if (msg == "登录成功") {
                if (ans == 1) {
                  bui.ajax({
                    url: config.apiurl + "api/v1.0/mychange",
                    data: {},
                    method: "post",
                    headers: {
                      'Content-Type': 'application/json'
                    }
                  }).done(function (res) {
                    var personlocation;
                    personlocation = res['loc'];

                    if (personlocation == null) {
                      personlocation = "";
                      bui.load({
                        url: "pages/registercontent/registercontent",
                        param: {
                          flag: "1",
                          personlocation: personlocation
                        }
                      });
                    }

                    config.bs.saveuser(data.mobile, data.password);
                    console.log(config.bs.getuser());
                    config.bs.login();

                    if (issave) {
                      config.bs.save();
                    } else {
                      config.bs.unsave();
                    }
                  }).fail(function (res, status) {
                    console.log(status); // status = "timeout" || "error" || "abort", "parsererror"
                  });
                } else {
                  config.bs.saveuser(data.mobile, data.password);
                  console.log(config.bs.getuser());
                  config.bs.login();

                  if (issave) {
                    config.bs.save();
                  } else {
                    config.bs.unsave();
                  }

                  bui.load({
                    url: "pages/success/success"
                  });
                }
              } else {
                bui.hint('登陆名或密码输入错误');
              }
            }, function (res, status) {
              bui.hint('请检查本机网络！');
            });
          };

          if (config.bs.islogin()) {
            console.log(config.bs.getuser());
            login(config.bs.getuser(), false);
          } else {
            if (config.bs.issave()) {
              var msg = config.bs.getuser();
              $("#user").attr("value", msg.mobile);
              $("#password").attr("value", msg.password);
            } // 登陆点击后，先进行手机号的验证


            $("#btn-login").on("click", function (e) {
              var mobile = $("#user").attr("value");
              var password = $("#password").attr("value");
              var issave = $("#rememberpwd").prop("checked"); // 手机号验证

              if (!phonecheck(mobile)) {
                bui.hint("请输入正确手机号");
              } else {
                // bui.load({ url: "pages/success/success"});
                var data_ = {
                  mobile: mobile,
                  // mobile
                  password: password // password

                };
                login(data_, issave);
              }
            });
          }

          $("#btn-register").on("click", function (e) {
            bui.load({
              url: "pages/register/register"
            });
          }); //微信登录相关代码

          $("#weixinLogin").on("click", function (i) {
            bui.load({
              url: "pages/isFirstUserbyWeiXin/isFirstUserbyWeiXin"
            });
            var data;

            var wxPlus = api.require('wxPlus');

            wxPlus.isInstalled(function (ret, err) {
              //用于填写授权	
              if (ret.isInstalled) {
                bui.hint('当前设备未安装微信');
                return;
              }

              wxPlus.auth({
                apiKey: 'wxcfb87d506d8ccd19'
              }, function (authRet, authRrr) {
                if (authRet.status) {
                  wxPlus.getToken({
                    apiKey: 'wxcfb87d506d8ccd19',
                    apiSecret: '659ceb669a10be86ba2fab6358498847',
                    code: authRet.code
                  }, function (tokenRet, tokenRrr) {
                    if (tokenRet.status) {
                      var n = {
                        openid: tokenRet.openId
                      };
                      n = JSON.stringify(n);
                      bui.ajax({
                        url: config.apiurl + "api/v1.0/openid",
                        data: n,
                        method: "post",
                        headers: {
                          "Content-Type": "application/json"
                        }
                      }).then(function (i) {
                        if (i.ans == 1) {
                          bui.load({
                            url: "pages/success/success"
                          });
                        } else if (i.ans == 0) {
                          //用户第一次用微信登录,因此要获取微信用户的信息留作备用
                          wxPlus.getUserInfo({
                            accessToken: tokenRet.accessToken,
                            openId: tokenRet.openId
                          }, function (infoRet, infoErr) {
                            if (infoRet.status) {
                              data = {
                                openid: infoRet.openid,
                                photo: infoRet.headimgurl
                              };
                              bui.load({
                                url: "pages/isFirstUserbyWeiXin/isFirstUserbyWeiXin",
                                param: {
                                  data: data
                                }
                              });
                            } else {
                              if (infoErr.code == 1) {
                                wxPlus.refreshToken({
                                  apiKey: 'wxcfb87d506d8ccd19',
                                  dynamicToken: tokenRet.dynamicToken
                                }, function (refRet, refErr) {
                                  if (refRet.status) {
                                    wxPlus.getUserInfo({
                                      accessToken: tokenRet.accessToken,
                                      openId: tokenRet.openId
                                    }, function (info2Ret, info2Err) {
                                      if (info2Ret.status) {
                                        data = {
                                          openid: info2Ret.openid,
                                          photo: info2Ret.headimgurl
                                        };
                                        bui.load({
                                          url: "pages/isFirstUserbyWeiXin/isFirstUserbyWeiXin",
                                          param: {
                                            data: data
                                          }
                                        });
                                      } else {
                                        bui.hint("用户信息获取失败");
                                      }
                                    });
                                  } else {
                                    //refresh重新刷新token失败
                                    bui.hint("用户信息获取失败");
                                  }
                                });
                              }

                              ;
                            }
                          });
                        } else if (i.ans == 2) {
                          var personlocation = "";
                          bui.load({
                            url: "pages/registercontent/registercontent",
                            param: {
                              flag: "1",
                              personlocation: personlocation
                            }
                          });
                        }
                      });
                    } else {
                      //getToken的失败，获取用户的唯一标识失败
                      bui.hint("微信登录失败");
                    }
                  });
                } else {
                  if (authRrr.code == 2) bui.hint("您已拒绝微信授权");else if (authRrr.code == 1) bui.hint("您已取消微信登录");else if (authRrr.code == 3) bui.hint("当前设备未安装微信");else bui.hint("微信登录发生未知错误");
                }
              });
            }); //
          });
          $("#test").on("click", function (i) {
            bui.load({
              url: "pages/isFirstUserbyWeiXin/isFirstUserbyWeiXin"
            });
          });
        };

        this.pageview.refresh = function () {
          location.reload();
        }; // 初始化


        this.pageview.init(); // 输出模块

        return this.pageview;
      },
      show: function show(e) {
        if (e.type == 'load' || e.type == 'back') this.pageview.refresh();
      }
    });
  }, {}]
}, {}, [1]);